package cn.edu.zjgsu.ito.controller;

import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.zjgsu.ito.model.OrderDetailInfomationView;
import cn.edu.zjgsu.ito.service.IAdminService;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.vo.OrderDetailInfomationVo;
import cn.edu.zjgsu.ito.vo.OrderStatisticalVo;
import cn.edu.zjgsu.ito.vo.Page;

@Controller
@RequestMapping(value = "/AdminController")
@Transactional
public class AdminOrderController
{

	@Autowired
	private IAdminService adminService;
	
	
	/**
	 * 根据订单orderId查找订单详情
	 * 
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "/orderDetailInfomation/{orderId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserDemandOrdersByUserId(
			@PathVariable int orderId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		/*OrderDetailInfomationVo orderDetailInfomationVo = adminService
				.getOrderDetailInfomationByOrderId(orderId);*/
		OrderDetailInfomationView orderDetailInfomationView = adminService.getOrderDetailInfomationView(orderId);
		result.put("code", Constant.OK);
		result.put("orderDetail", orderDetailInfomationView);
		return result;
	}
	
	

	
	/**
	 * 分页根据userId按条件查询用户请求订单
	 * @param userId
	 * @param sequence
	 * @param selectType
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/userDemandOrders/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserDemandOrdersByUserId(
			@RequestParam(name = "userId") int userId,
			@RequestParam(name = "sequence") int sequence,
			@RequestParam(name="selectType",required=false)String selectType,
			@RequestParam(name="beginTime",required=false)Date beginTime,
			@RequestParam(name="endTime",required=false)Date endTime)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		
		int selectTypeInt =0;
		if(selectType!=null)
			selectTypeInt = Integer.valueOf(selectType);
		
		
		List<OrderDetailInfomationView> OrderDetailInfomationViews = adminService
				.getDemandOrderByUserId(userId, sequence,selectTypeInt,beginTime,endTime);
		result.put("code", Constant.OK);
		result.put("orderStatisticalVos",OrderDetailInfomationViews);
		return result;
	}
	
	
	
	
	
	
	/**
	 * 按条件和用户userId分页查询用户代购订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param userId
	 * @param sequence
	 * @param key
	 * @return
	 */
	@RequestMapping(value = "/userPurcaseOrders/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserPurchaseOrdersByUserId(
			@RequestParam("userId") int userId,
			@RequestParam("sequence") int sequence,
			@RequestParam(name="selectType",required=false)String selectType,
			@RequestParam(name="beginTime",required=false)Date beginTime,
			@RequestParam(name="endTime",required=false)Date endTime)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		
		int selectTypeInt =0;
		if(selectType!=null)
			selectTypeInt = Integer.valueOf(selectType);
		
		List<OrderDetailInfomationView> OrderDetailInfomationViews = adminService
				.getPurcaseOrderByUserId(userId, sequence, selectTypeInt,beginTime,endTime);
		result.put("code", Constant.OK);
		result.put("orderStatisticalVos", OrderDetailInfomationViews);
		return result;
	}
	
	
	
	
	
	
	
	
	/**
	 * 分页根据用户id按条件查询用户所有订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param adminId
	 * @param sequence
	 * @param key
	 * @return
	 */
	@RequestMapping(value = "/userAllOrders/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserOrdersByUserId(
			@RequestParam("userId") int userId,
			@RequestParam("sequence") int sequence,
			@RequestParam(name="selectType",required=false)String selectType,
			@RequestParam(name="beginTime",required=false)Date beginTime,
			@RequestParam(name="endTime",required=false)Date endTime)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		/*Page<OrderStatisticalVo> orderStatisticalVos = adminService
				.getAllOrderByUserId(userId, sequence, key, pageCount, pageSize);*/


		int selectTypeInt =0;
		if(selectType!=null)
			selectTypeInt = Integer.valueOf(selectType);
		
		List<OrderDetailInfomationView> OrderDetailInfomationViews = adminService
				.getAllOrderByUserId(userId, sequence, selectTypeInt, beginTime, endTime);
		
		result.put("code", Constant.OK);
		result.put("orderStatisticalVos", OrderDetailInfomationViews);
		return result;
	}
	
	
	
	/**
	 * 按条件分页查询所有订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 * @param key
	 * @return
	 */
	@RequestMapping(value = "/orders/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAllOrders(
			@RequestParam("sequence") int sequence,
			@RequestParam(name="selectType",required=false)String selectType,
			@RequestParam(name="beginTime",required=false)Date beginTime,
			@RequestParam(name="endTime",required=false)Date endTime)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		/*List<OrderStatisticalVo> orderStatisticalVos = adminService
				.getAllOrders(sequence, key, pageCount, pageSize);*/
		
		int selectTypeInt =0;
		if(selectType!=null)
			selectTypeInt = Integer.valueOf(selectType);
		List<OrderDetailInfomationView> OrderDetailInfomationViews = 
				adminService.getAllOrders(sequence, selectTypeInt, beginTime, endTime);
		result.put("code", Constant.OK);
		result.put("orderStatisticalVos", OrderDetailInfomationViews);
		return result;
	}
	
	
	
	
	
	/**
	 * 根据快递员courierId分页查询订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param courier
	 * @param sequence
	 * @param key
	 * @return
	 */
	@RequestMapping(value = "/courierOrder/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getOrdersByCourierId(
			@RequestParam(name = "courierId") int courierId,
			@RequestParam(name = "sequence") int sequence)
	{
		Map<String, Object> result = new HashMap<String, Object>();

		/*
		 * Page<OrderStatisticalVo> OrderStatisticalVos = adminService
		 * .getOrderByCourierId(courierId, sequence, key, pageCount, pageSize);
		 */

		List<OrderDetailInfomationView> orderDetailInfomationViews = adminService
				.getAllOrderByCourierId(courierId, sequence);

		result.put("orderStatisticalVos", orderDetailInfomationViews);
		return success(result);
	}
	
	
	
	
	
	
	
	
	
	

	/**
	 * 按条件分页查询所有订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param sequence
	 * @param key
	 * @return
	 *//*
	@RequestMapping(value = "/orders/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAllOrders(@PathVariable int pageCount,
			@PathVariable int pageSize, @RequestParam("sequence") int sequence,
			@RequestParam("key") String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<OrderStatisticalVo> orderStatisticalVos = adminService
				.getAllOrders(sequence, key, pageCount, pageSize);
		
		Page<OrderDetailInfomationView> page = adminService.getAllOrders(sequence, key, pageCount, pageSize);
		result.put("code", Constant.OK);
		result.put("orderStatisticalVos", page.getData());
		result.put("count", page.getTotalCount());
		return result;
	}*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	/*
	 * @RequestMapping(value =
	 * "/admins/pageCount/{pageCount}/pageSize/{pageSize}", method =
	 * RequestMethod.GET)
	 * 
	 * @ResponseBody public Map<String, Object> getAdmins(@PathVariable int
	 * pageCount,
	 * 
	 * @PathVariable int pageSize,
	 * 
	 * @RequestParam(name = "sequence") int sequence,
	 * 
	 * @RequestParam(name = "key", required = false) String key) { Map<String,
	 * Object> result = new HashMap<String, Object>(); List<AdminVo> admins =
	 * adminService.getAdminsWithPaging(pageCount, pageSize, sequence, key ==
	 * null ? "" : key); result.put("code", Constant.OK); result.put("admins",
	 * admins); result.put("count", adminService.selectCount(5, 0, key,
	 * sequence)); return result; }
	 */

	
	
	/**
	 * 根据订单orderId关闭订单
	 * 
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "closeOrderByOrderId/{orderId}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> closeOrderByOrderId(@PathVariable int orderId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.changeOrderStateByOrderId(orderId,7);
		return success(result);
	}
	
	
	
	
	
	/**
	 * 批量关闭订单
	 * 
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = "closeOrders", method = RequestMethod.POST)
	@ResponseBody
	@Transactional
	public Map<String, Object> closeOrdersByOrderId(@RequestBody Map map)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<Integer> orderIdList = (List<Integer>) map.get("orderIds");
		for (int orderId : orderIdList)
		{
			adminService.changeOrderStateByOrderId(orderId,7);
		}
		return success(result);
	}
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 根据订单orderId打开订单
	 * 
	 * @param orderId
	 * @return
	 *//*
	@RequestMapping(value = "openOrderByOrderId/{orderId}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> openOrderByOrderId(@PathVariable int orderId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.changeOrderStateByOrderId(orderId,3);
		return success(result);
	}*/
	

	

	private Map<String, Object> error(Map<String, Object> result)
	{
		result.put("code", Constant.FAIL);
		return result;
	}

	private Map<String, Object> success(Map<String, Object> result)
	{
		result.put("code", Constant.OK);
		return result;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	/**
	 * 分页根据userId按条件查询用户请求订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param adminId
	 * @param sequence
	 * @param key
	 * @return
	 */
	/*
	 * @RequestMapping(value =
	 * "/userDemandOrders/pageCount/{pageCount}/pageSize/{pageSize}", method =
	 * RequestMethod.GET)
	 * 
	 * @ResponseBody public Map<String, Object> getUserDemandOrdersByUserId(
	 * 
	 * @PathVariable(name = "pageCount") int pageCount,
	 * 
	 * @PathVariable(name = "pageSize") int pageSize,
	 * 
	 * @RequestParam(name = "userId") int userId,
	 * 
	 * @RequestParam(name = "sequence") int sequence,
	 * 
	 * @RequestParam(name = "key", required = false) String key) { Map<String,
	 * Object> result = new HashMap<String, Object>(); List<OrderStatisticalVo>
	 * orderStatisticalVos = adminService .getDemandOrderByUserId(userId,
	 * sequence, key == null ? "" : key, pageCount, pageSize);
	 * result.put("code", Constant.OK); result.put("orderStatisticalVos",
	 * orderStatisticalVos); result.put("count", adminService.selectCount(1,
	 * userId, key, sequence)); return result; }
	 */

	
	
	/**
	 * 分页根据userId按条件查询用户请求订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param adminId
	 * @param sequence
	 * @param key
	 * @return
	 *//*
	@RequestMapping(value = "/userDemandOrders/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserDemandOrdersByUserId(
			@PathVariable(name = "pageCount") int pageCount,
			@PathVariable(name = "pageSize") int pageSize,
			@RequestParam(name = "userId") int userId,
			@RequestParam(name = "sequence") int sequence,
			@RequestParam(name = "key", required = false) String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		
		Page<OrderDetailInfomationView> page = adminService
				.getDemandOrderByUserId(userId, sequence, key == null ? ""
						: key, pageCount, pageSize);
		result.put("code", Constant.OK);
		result.put("orderStatisticalVos", page.getData());
		result.put("count", page.getTotalCount());
		return result;
	}*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	

	/**
	 * 按条件和用户userId分页查询用户代购订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param userId
	 * @param sequence
	 * @param key
	 * @return
	 */
	/*
	 * @RequestMapping(value =
	 * "/userPurcaseOrders/pageCount/{pageCount}/pageSize/{pageSize}", method =
	 * RequestMethod.GET)
	 * 
	 * @ResponseBody public Map<String, Object> getUserPurchaseOrdersByUserId(
	 * 
	 * @PathVariable int pageCount, @PathVariable int pageSize,
	 * 
	 * @RequestParam("userId") int userId,
	 * 
	 * @RequestParam("sequence") int sequence,
	 * 
	 * @RequestParam("key") String key) { Map<String, Object> result = new
	 * HashMap<String, Object>(); List<OrderStatisticalVo> orderStatisticalVos =
	 * adminService .getPurcaseOrderByUserId(userId, sequence, key, pageCount,
	 * pageSize); result.put("code", Constant.OK);
	 * result.put("orderStatisticalVos", orderStatisticalVos);
	 * result.put("count", adminService.selectCount(2, userId, key, sequence));
	 * return result; }
	 */

	/**
	 * 按条件和用户userId分页查询用户代购订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param userId
	 * @param sequence
	 * @param key
	 * @return
	 *//*
	@RequestMapping(value = "/userPurcaseOrders/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserPurchaseOrdersByUserId(
			@PathVariable int pageCount, @PathVariable int pageSize,
			@RequestParam("userId") int userId,
			@RequestParam("sequence") int sequence,
			@RequestParam("key") String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		Page<OrderDetailInfomationView> page = adminService
				.getPurcaseOrderByUserId(userId, sequence, key, pageCount,
						pageSize);
		result.put("code", Constant.OK);
		result.put("orderStatisticalVos", page.getData());
		result.put("count", page.getTotalCount());
		return result;
	}*/

	/**
	 * 分页根据用户id按条件查询用户所有订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param adminId
	 * @param sequence
	 * @param key
	 * @return
	 */
	/*
	 * @RequestMapping(value =
	 * "/userAllOrders/pageCount/{pageCount}/pageSize/{pageSize}", method =
	 * RequestMethod.GET)
	 * 
	 * @ResponseBody public Map<String, Object> getUserOrdersByUserId(
	 * 
	 * @PathVariable int pageCount, @PathVariable int pageSize,
	 * 
	 * @RequestParam("userId") int userId,
	 * 
	 * @RequestParam("sequence") int sequence,
	 * 
	 * @RequestParam("key") String key) { Map<String, Object> result = new
	 * HashMap<String, Object>(); List<OrderStatisticalVo> orderStatisticalVos =
	 * adminService .getAllOrderByUserId(userId, sequence, key, pageCount,
	 * pageSize); result.put("code", Constant.OK);
	 * result.put("orderStatisticalVos", orderStatisticalVos);
	 * result.put("count", adminService.selectCount(3, userId, key, sequence));
	 * return result; }
	 */

	
	/**
	 * 分页根据用户id按条件查询用户所有订单
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @param adminId
	 * @param sequence
	 * @param key
	 * @return
	 *//*
	@RequestMapping(value = "/userAllOrders/pageCount/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserOrdersByUserId(
			@PathVariable int pageCount, @PathVariable int pageSize,
			@RequestParam("userId") int userId,
			@RequestParam("sequence") int sequence,
			@RequestParam("key") String key)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		Page<OrderStatisticalVo> orderStatisticalVos = adminService
				.getAllOrderByUserId(userId, sequence, key, pageCount, pageSize);

		Page<OrderDetailInfomationView> page = adminService
				.getAllOrderByUserId(userId, sequence, key, pageCount, pageSize);
		
		result.put("code", Constant.OK);
		result.put("orderStatisticalVos", page.getData());
		result.put("count", page.getTotalCount());
		return result;
	}*/

}
